package com.mark.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 登录
 * @author MARKCC
 * @date 2018/10/11-17:04
 * @email markerccc@163.com
 */
@Controller
public class LoginController {
    /**
     * PostMapping: 处理post请求
     * @RequestMapping (value = "/user/login", method = RequestMethod.POST)
     * @return
     */
    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map,
                        HttpSession session) {
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            //登录成功, 防止表单重复提交, 可以重定向到主页, redirect: 重定向
            session.setAttribute("loginUser", username);
            return "redirect:/main.html";
        }else{
            // 登录失败
            map.put("msg", "用户名密码错误");
            return "/login.html";
        }
    }
}
